<template>
  <div>
    <vxe-toolbar>
      <template #buttons>
        <vxe-button @click="addNewExplain">新增</vxe-button>
        <vxe-button @click="removeCheckboxRow()">删除选中</vxe-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      border
      resizable
      show-overflow
      :data="data"
      ref="xTable"
      keep-source
      size="small"
      :loading="loading"
      :edit-config="{ trigger: 'manual', mode: 'row' }"
    >
      <vxe-table-column type="checkbox" width="60"></vxe-table-column>
      <vxe-table-column
        field="pid"
        title="属性"
        :edit-render="{
          name: '$select',
          options: pNames,
        }"
      ></vxe-table-column>
      <vxe-table-column
        field="sid"
        :edit-render="{
          name: '$select',
        }"
        title="子属性"
      ></vxe-table-column>
      <!--
      <vxe-table-column
        field="index"
        title="索引"
        :edit-render="{
          name: '$input',
          props: { type: 'int' },
        }"
      ></vxe-table-column>-->

      <vxe-table-column
        field="modbus_param.begin"
        title="开始地址"
        :edit-render="{
          name: '$input',
          props: { type: 'number' },
        }"
      ></vxe-table-column>
      <vxe-table-column
        field="modbus_param.count"
        title="个数"
        :edit-render="{
          name: '$input',
          props: { type: 'number' },
        }"
      ></vxe-table-column>
      <!--
      <vxe-table-column
        field="mobbus_param.code"
        title="功能码"
        :edit-render="{ name: 'select', options: code }"
      ></vxe-table-column>-->
      <vxe-table-column
        field="modbus_param.bigend"
        title="是否大端"
        :edit-render="{
          name: '$select',
          options: trueFalse,
        }"
      ></vxe-table-column>
      <vxe-table-column
        field="modbus_param.swap"
        title="高低互换"
        :edit-render="{
          name: '$select',
          options: trueFalse,
        }"
      ></vxe-table-column>
      <vxe-table-column
        field="modbus_param.number"
        title="乘法因子"
        :edit-render="{
          name: '$input',
          props: { type: 'number', placeholder: '请输入数值' },
        }"
      ></vxe-table-column>
      <vxe-table-column
        field="modbus_param.func"
        title="解析函数"
        :edit-render="{
          name: '$input',
        }"
      ></vxe-table-column>
      <vxe-table-column title="操作" width="160">
        <template #default="{ row }">
          <template v-if="$refs.xTable.isActiveByRow(row)">
            <vxe-button @click="saveRowEvent(row)">保存</vxe-button>
            <vxe-button @click="cancelRowEvent(row)">取消</vxe-button>
          </template>
          <template v-else>
            <vxe-button @click="editRowEvent(row)">编辑</vxe-button>
          </template>
        </template>
      </vxe-table-column>
    </vxe-table>
  </div>
</template>
<script>
import { number } from "echarts";
export default {
  name: "ExplainTable",
  props: ["data", "mbody", "code"],
  data() {
    return {
      loading: false,
      trueFalse: [
        { label: "是", value: true },
        { label: "否", value: false },
      ],
    };
  },
  computed: {
    pNames() {
      if (this.mbody && this.mbody.properties) {
        let names = this.mbody.properties.map((p) => {
          return { label: p.name, value: p.identifier };
        });
        console.log("&&&&&&&&&&", names);
        return names;
      }
      return [];
    },
    subNames() {
      return [];
    },
  },
  methods: {
    init(row) {
      if (row) {
        if (!row.pid) {
          row.pid = "";
        }
        if (!row.sid) {
          row.sid = "";
        }
        if (!row.index) {
          row.index = 0;
        }
        if (!row.modbus_param.count) {
          row.modbus_param.count = 1;
        }
        if (row.modbus_param.offset == undefined) {
          row.modbus_param.offset = 0;
        }
        if (row.modbus_param.swap == undefined) {
          row.modbus_param.swap = true;
        }
        if (row.modbus_param.bigend == undefined) {
          row.modbus_param.bigend = false;
        }
        if (row.modbus_param.func == undefined) {
          row.modbus_param.func = "";
        }
        if (row.modbus_param.begin == undefined) {
          row.modbus_param.begin = 0;
        }
        if (row.modbus_param.number == undefined) {
          row.modbus_param.number = 1;
        }
        if (row.modbus_param.additive == undefined) {
          row.modbus_param.additive = 0;
        }
        
      }
    },
    correctData() {
      let self = this;
      setTimeout(() => {
        self.data.forEach((d) => {
          if (d.modbus_param.number == "") {
            // d.modbus_param.number = undefined
            d.modbus_param.number = 1
          }
          if (typeof d.modbus_param.number == "string") {
            d.modbus_param.number = parseFloat(d.modbus_param.number);
          }
          if (typeof d.modbus_param.begin == "string") {
            d.modbus_param.begin = parseInt(d.modbus_param.begin);
          }
          if (typeof d.modbus_param.count == "string") {
            d.modbus_param.count = parseInt(d.modbus_param.count);
          }

          if (typeof d.modbus_param.additive == "string"){
            d.modbus_param.additive = parseFloat(d.modbus_param.additive);
          }
        });
      }, 300);
    },
    editRowEvent(row) {
      this.init(row);
      const $table = this.$refs.xTable;
      $table.setActiveRow(row);
    },
    saveRowEvent() {
      const $table = this.$refs.xTable;
      let self = this;
      $table.clearActived().finally(() => {
        self.correctData();
      });
    },
    cancelRowEvent(row) {
      const $table = this.$refs.xTable;
      $table.clearActived().then(() => {
        // 还原行数据
        $table.revertData(row);
      });
    },
    addNewExplain() {
      let newParam = {};
      let newRow = { modbus_param: newParam };
      this.init(newRow);
      this.data.push(newRow);
      this.editRowEvent(newRow);
    },
    removeCheckboxRow() {
      let selectRecords = this.$refs.xTable.getCheckboxRecords();
      console.log("------------------", selectRecords);
      let self = this;
      selectRecords.forEach((r) => {
        let i = self.data.findIndex((d) => {
          return d.modbus_param == r.modbus_param;
        });
        if (i >= 0) {
          self.data.splice(i, 1);
        }
      });

      this.$refs.xTable.removeCheckboxRow();
    },
  },
};
</script>
<style scoped>
.vxe-select--panel {
  z-index: 9997 !important;
}
</style>